//
//  HOViewStatusViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOViewStatusViewController.h"
#import "HOReportListTableViewCell.h"
#import "HOStatusView.h"
#import "HOUtils.h"
#import "HOStatusTableViewCell.h"
#import "HOReportOrderEntity.h"
#import "HOConstants.h"
#import "HOStatusEntity.h"
#import "HORepairerDetailViewController.h"
#import "UIImageView+WebCache.h"

@interface HOViewStatusViewController ()
{
	
}
@property(nonatomic, retain)HOStatusView *statusView;
@property(nonatomic, retain)HOStatusEntity *statusEntity;
@end

@implementation HOViewStatusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	[self initSubviews];
	self.statusEntity = [[[HOStatusEntity alloc] init] autorelease];
}

- (void)initSubviews
{
	self.statusView = [[[NSBundle mainBundle] loadNibNamed:@"HOStatusView" owner:self options:nil] firstObject];
	self.statusView.frame = CGRectMake(0,
										   0,
										   self.view.frame.size.width,
										   232);
}

- (void)requestData
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_CheckOrderStatus,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderEntity.orderId]}];
	
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"查看进度", @"查看进度")];
	[self.statusView setEntity:self.orderEntity];
	[self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
	self.statusEntity = nil;
	self.orderEntity = nil;
	self.statusView = nil;
	[super dealloc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
		return 100;
	if(indexPath.section == 1)
	{
		if(self.orderEntity.doneId == 0)
			return 0;
		return 58;
	}
	if(indexPath.section == 2)
		return 58;
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(section == 2)
	{
		return 4;
	}
	if(section == 1)
	{
		if(self.orderEntity.doneId == 0)
			return 0;
		return 1;
	}
    return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return [HOUtils defaultOnePixelConversion];
	
	else if(section == 1)
	{
		if(self.orderEntity.doneId == 0)
			return [HOUtils defaultOnePixelConversion];
		return SECTION_PADDING / 2;
	}
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	if(indexPath.section == 0)
	{
		static NSString *ReporterTableViewCellIdentifie = @"ReporterTableViewCellIdentifie";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ReporterTableViewCellIdentifie];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: ReporterTableViewCellIdentifie] autorelease];
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
		
		UIImageView *headerImgView = (UIImageView *)[cell viewWithTag:13];
		if(headerImgView == nil)
		{
			UIImage *image = [UIImage imageNamed:@"chakanzhuangtai_touxiang"];
			headerImgView = [[[UIImageView alloc]
							  initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
													   (cellHeight - 78) / 2,
													   78,
													   78)] autorelease];
			headerImgView.tag = 13;
			[headerImgView setImage:image];
			[cell.contentView addSubview:headerImgView];
		}
		
		NSURL *imgURL = [NSURL URLWithString:[HOUtils getValideString:self.statusEntity.image]];
		[headerImgView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"morentu"]];
	
		UILabel *statusTitleLbl = (UILabel *)[cell viewWithTag:115];
		if(statusTitleLbl == nil)
		{
			statusTitleLbl = [[[UILabel alloc]
						initWithFrame:CGRectMake(HO_HORIZONAL_PADDING + headerImgView.frame.size.width + HO_HORIZONAL_PADDING,
												 HO_HORIZONAL_PADDING,
												 tableView.frame.size.width - 108,
												 22)] autorelease];
			statusTitleLbl.tag = 115;
			statusTitleLbl.font = [UIFont systemFontOfSize:15];
			statusTitleLbl.textColor = TEXT_COLOR_666666;
			[cell.contentView addSubview:statusTitleLbl];
		}
		statusTitleLbl.text = NSLocalizedString(@"订单状态：", @"订单状态：");
		[statusTitleLbl sizeToFit];
		statusTitleLbl.frame = CGRectMake(HO_HORIZONAL_PADDING + headerImgView.frame.size.width + HO_HORIZONAL_PADDING,
										  HO_HORIZONAL_PADDING,
										  statusTitleLbl.frame.size.width,
										  22);
		
		UILabel *statusLbl = (UILabel *)[cell viewWithTag:15];
		if(statusLbl == nil)
		{
			statusLbl = [[[UILabel alloc]
						  initWithFrame:CGRectMake(statusTitleLbl.frame.size.width + statusTitleLbl.frame.origin.x,
												   statusTitleLbl.frame.origin.y,
												   tableView.frame.size.width - 250,
												   22)] autorelease];
			statusLbl.tag = 15;
			statusLbl.font = [UIFont systemFontOfSize:15];
			[cell.contentView addSubview:statusLbl];
		}
		HOStatusEntity *entity = [self.statusEntity.subStatusList lastObject];
		if(entity != nil)
		{
			NSString *statusStr = [HOUtils getDescribtionWithStatus:entity.status];
			if(self.orderEntity.isUncompleted)
			{
				statusStr = @"未完成";
			}
			statusLbl.text = statusStr;
		}
		else
		{
			statusLbl.text = @"";
		}
		statusLbl.textColor = [HOUtils getOrderColorWithStatus:entity.status];
		
		UILabel *orderSNLbl = (UILabel *)[cell viewWithTag:25];
		if(orderSNLbl == nil)
		{
			orderSNLbl = [[[UILabel alloc]
						  initWithFrame:CGRectMake(statusTitleLbl.frame.origin.x,
												   statusLbl.frame.size.height + statusLbl.frame.origin.y,
												   tableView.frame.size.width - 108,
												   22)] autorelease];
			orderSNLbl.tag = 25;
			orderSNLbl.font = [UIFont systemFontOfSize:13];
			orderSNLbl.textColor = TEXT_COLOR_666666;
			[cell.contentView addSubview:orderSNLbl];
		}
		orderSNLbl.text = [NSString stringWithFormat:@"%@%d", NSLocalizedString(@"报修单号：", @"报修单号："), self.statusEntity.orderSN];
		
		UILabel *contentLbl = (UILabel *)[cell viewWithTag:35];
		if(contentLbl == nil)
		{
			contentLbl = [[[UILabel alloc]
						   initWithFrame:CGRectMake(statusTitleLbl.frame.origin.x,
													orderSNLbl.frame.size.height + orderSNLbl.frame.origin.y,
													tableView.frame.size.width - 108,
													37)] autorelease];
			contentLbl.tag = 35;
			contentLbl.numberOfLines = 2;
//			contentLbl.layer.borderWidth = 1;
			contentLbl.font = [UIFont systemFontOfSize:13];
			contentLbl.textColor = TEXT_COLOR_999999;
			[cell.contentView addSubview:contentLbl];
		}
		contentLbl.text = self.statusEntity.detail;
//		[contentLbl sizeToFit];
//		contentLbl.frame = CGRectMake(statusTitleLbl.frame.origin.x,
//									  tableView.frame.size.width - 108,
//									  statusLbl.frame.size.width,
//									  MAX(22, contentLbl.frame.size.height));
		return cell;
	}
	else if(indexPath.section == 1)
	{
		static NSString *ReporterTableViewCellIdentifie = @"ReporterTableViewCellIdentifie";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ReporterTableViewCellIdentifie];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: ReporterTableViewCellIdentifie] autorelease];
		}
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		UIImageView *headerImgView = (UIImageView *)[cell viewWithTag:13];
		if(headerImgView == nil)
		{
			UIImage *image = [UIImage imageNamed:@"chakanzhuangtai_touxiang"];
			headerImgView = [[[UIImageView alloc]
							 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
													  5,
													  image.size.width,
													  image.size.height)] autorelease];
			headerImgView.tag = 13;
			[headerImgView setImage:image];
			[cell.contentView addSubview:headerImgView];
			[headerImgView.layer setMasksToBounds:YES];
			headerImgView.layer.cornerRadius = image.size.width / 2;
			headerImgView.layer.cornerRadius = headerImgView.frame.size.width / 2;
		}
		[headerImgView sd_setImageWithURL:[NSURL URLWithString:self.statusEntity.repairmanHeadpic] placeholderImage:[UIImage imageNamed:@"chakanzhuangtai_touxiang"] options:SDWebImageCacheMemoryOnly];
		
		
		UILabel *workStatusLbl = (UILabel *)[cell viewWithTag:1123];
		if(workStatusLbl == nil)
		{
			workStatusLbl = [[[UILabel alloc]
							  initWithFrame:CGRectMake(12,
													   37,
													   40,
													   22)] autorelease];
			workStatusLbl.tag = 1123;
			[cell.contentView addSubview:workStatusLbl];
			workStatusLbl.textColor = TEXT_COLOR_181818;
			workStatusLbl.font = [UIFont systemFontOfSize:12];
		}
		if(self.statusEntity.isWorking)
		{
			workStatusLbl.text = @"[在线]";
			workStatusLbl.textColor = UIColorFromRGB(0x0b7f1e);
		}
		else
		{
			workStatusLbl.text = @"[离线]";
			workStatusLbl.textColor = UIColorFromRGB(0xcdcdcd);
		}
		
		
		
		UILabel *nameLbl = (UILabel *)[cell viewWithTag:15];
		if(nameLbl == nil)
		{
			nameLbl = [[[UILabel alloc]
							  initWithFrame:CGRectMake(HO_HORIZONAL_PADDING + headerImgView.frame.size.width + HO_HORIZONAL_PADDING,
													   0,
													   tableView.frame.size.width - 60,
													   40)] autorelease];
			nameLbl.tag = 15;
			nameLbl.font = [UIFont systemFontOfSize:13];
			nameLbl.textColor = TEXT_COLOR_999999;
			[cell.contentView addSubview:nameLbl];
		}
		nameLbl.text = [HOUtils getValideString:self.statusEntity.repairmanName];
		
		
		UILabel *locationLbl = (UILabel *)[cell viewWithTag:116];
		if(locationLbl == nil)
		{
			locationLbl = [[[UILabel alloc]
							  initWithFrame:CGRectMake(nameLbl.frame.origin.x,
													   23,
													   tableView.frame.size.width - 60,
													   40)] autorelease];
			locationLbl.tag = 116;
			locationLbl.font = [UIFont systemFontOfSize:12];
			locationLbl.textColor = TEXT_COLOR_999999;
			[cell.contentView addSubview:locationLbl];
		}
		locationLbl.text = self.statusEntity.repairmanDepName;
		
		
		UIButton *iconImgView = (UIButton *)[cell viewWithTag:118];
		if(iconImgView == nil)
		{
			UIImage *image = [UIImage imageNamed:@"ic-chakanzhuangtai_dadianhua"];
			iconImgView = [[[UIButton alloc]
							  initWithFrame:CGRectMake(tableView.frame.size.width - 35 - 30,
													   (cellHeight - 35) / 2,
													   35,
													   35)] autorelease];
			iconImgView.tag = 118;
			[iconImgView setBackgroundImage:image forState:UIControlStateNormal];
//			iconImgView.layer.borderWidth = 1;
			[iconImgView addTarget:self action:@selector(callPhoneNumber:) forControlEvents:UIControlEventTouchUpInside];
			[cell.contentView addSubview:iconImgView];
		}
//		if(self.statusEntity.virtualCellPhone == nil || self.statusEntity.virtualCellPhone.length < 2)
//		{
//			iconImgView.hidden = YES;
//		}
//		else
//		{
//			iconImgView.hidden = NO;
//		}
		return cell;
		
	}
	else if(indexPath.section == 2)
	{
		static NSString *HOReplyTableViewCell = @"HOReplyTableViewCell";
		HOStatusTableViewCell *cell = (HOStatusTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOReplyTableViewCell];
		if(cell == nil)
		{
			cell = [[[HOStatusTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOReplyTableViewCell] autorelease];
			[cell initWithWidth:tableView.frame.size.width withHeight:58];
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;

		HOStatusEntity *statusEntity = [self.statusEntity.subStatusDic objectForKey:[NSString stringWithFormat:@"%d", (int)(indexPath.row + 1)]];
//		if(indexPath.row < [self.statusEntity.subStatusList count])
//		{
//			statusEntity = [self.statusEntity.subStatusList objectAtIndex:indexPath.row];
//		}
		HOStatusEntity *lastEntity = [self.statusEntity.subStatusList lastObject];
		if(indexPath.row == 0)
		{
			cell.statusLbl.text = NSLocalizedString(@"未受理", @"未受理");
		}
		else if(indexPath.row == 1)
		{
			cell.statusLbl.text = NSLocalizedString(@"已受理", @"已受理");
		}
		else if(indexPath.row == 2)
		{
			cell.statusLbl.text = NSLocalizedString(@"维修完成", @"维修完成");
		}
		else if(indexPath.row == 3)
		{
			cell.statusLbl.text = NSLocalizedString(@"确认完成", @"确认完成");
		}
		if(statusEntity)
		{
			cell.contentLbl.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"维修人:", @"维修人:"), statusEntity.repairmanName];
			cell.timeLbl.text = [HOUtils getDateFromTimeInterval:statusEntity.createTime * 1000];
			cell.contentLbl.hidden = NO;
			cell.timeLbl.hidden = NO;
		}
		if(lastEntity != nil && lastEntity.status > (indexPath.row + 1))
		{
			[cell setHightLight:YES];
		}
		else
		{
			[cell setHightLight:statusEntity != nil];
		}
		return cell;
	}
	return nil;
}

- (void)callPhoneNumber:(id)sender
{
	[HOUtils callPhoneNumber:self.statusEntity.virtualCellPhone];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(indexPath.section == 1)
	{
		HORepairerDetailViewController *viewCtrl = [[HORepairerDetailViewController alloc] init];
		viewCtrl.userId = self.orderEntity.doneId;
//		viewCtrl.isWorking = entity.isWorking;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
//		NSString *mobile = [HOUtils getValideString:self.orderEntity.repairmanMobile];
//		if(mobile.length > 0)
//		{
//			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", mobile]]];
//
//		}
	}
}

#pragma mark

- (HOEditTableViewCell *)tableView:(UITableView *)tableView cellForEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOEditTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	return cell;
}
#pragma mark -- tool

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_CheckOrderStatus])
	{
		[self.statusEntity parseDic:response.dataNode];
		[baseTableview reloadData];
	}
}
@end
